﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gpsys.Web.Models;
using Gpsys.Web.Common;

namespace Gpsys.Web.Controllers
{
    public class TransformController : BaseController
    {
        private void SetSelectedMenu()
        {
            ViewBag.SelectedMenu = "Transform";
        }

        //
        // GET: /Workflow/

        public ActionResult Index()
        {
            SetSelectedMenu();
            TransformModel wfModel = new TransformModel();
            return View(wfModel);
        }

        public ActionResult Create()
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create Transform";
            ViewBag.IsUpdate = false;
            TransformEntityModel model = new TransformEntityModel()
            {
                Transform = new Entity.MSG.Transform()
            };
            return View("Edit", model);
        }

        [HttpPost]
        [ValidateInput(enableValidation: false)]
        public ActionResult Create(TransformEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Create Transform";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = false;

            TransformModel wfModel = new TransformModel();

            wfModel.ValidateTransform(ModelState, model);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {
                model.Transform.CreatedBy = UserHelpers.UserName;
                model.Transform.ModifiedBy = UserHelpers.UserName;

                if (wfModel.InsertTransform(model.Transform))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        [HttpPost]
        [ValidateInput(enableValidation: false)]
        public ActionResult Edit(TransformEntityModel model)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit Transform";
            ViewBag.Valid = true;
            ViewBag.IsUpdate = true;

            TransformModel wfModel = new TransformModel();

            wfModel.ValidateTransform(ModelState, model);

            ViewBag.Valid = ModelState.IsValid;

            if (ModelState.IsValid)
            {

                model.Transform.ModifiedBy = UserHelpers.UserName;

                if (wfModel.UpdateTransform(model.Transform))
                {
                    return RedirectToAction("Index");
                }
            }

            return View("Edit", model);
        }

        public ActionResult Edit(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Edit Transform";

            ViewBag.IsUpdate = true;

            TransformModel wfModel = new TransformModel();
            var wf = wfModel.GetTransform(Id);
            if (wf != null)
            {
                TransformEntityModel m = new TransformEntityModel()
                {
                    Transform = wf
                };
                return View("Edit", m);
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult Detail(int Id)
        {
            SetSelectedMenu();
            ViewBag.FormTitle = "Detail Transform";

            TransformModel wfModel = new TransformModel();
            var wf = wfModel.GetTransform(Id);
            if (wf != null)
            {
                TransformEntityModel m = new TransformEntityModel()
                {
                    Transform = wf
                };
                return View(m);
            }

            return RedirectToAction("e404", "Error");
        }

        public ActionResult Delete(int Id)
        {
            SetSelectedMenu();

            TransformModel wfModel = new TransformModel();

            var wf = wfModel.GetTransform(Id);
            if (wf != null)
            {
                wfModel.DeleteTransform(Id);

                return RedirectToAction("Index");
            }

            return RedirectToAction("e404", "Error");
        }

    }
}
